package cn.com.huzo.webapp.controller.purchase;

import cn.com.huzo.model.entities.purchase.NonMaterialCostHeaderEntity;
import cn.com.huzo.service.purchase.NonMaterialCostHeaderManager;
import cn.com.huzo.service.purchase.NonMaterialCostLinesManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 非材料花费画面控制器
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/purchase/non_material_cost")
public class NonMaterialCostController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "NonMaterialCostHeaderManager")
    private NonMaterialCostHeaderManager _manager_header;

    @Resource(name = "NonMaterialCostLinesManager")
    private NonMaterialCostLinesManager _manager_lines;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到非材料花费一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("purchase.non_material_cost.list.def");
        return view;
    }

    /**
     * 跳转非材料花费编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作类别 1：增加 2：编辑
     * @param headerId 主表ID
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/{action}/{headerId}/edit.html")
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "headerId") Long headerId) throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.addObject("action", action);
        view.addObject("headerId", headerId);
        int thisYear = Calendar.getInstance().get(Calendar.YEAR); // 得到今年
        List<Integer> yearList = new ArrayList();
        thisYear = thisYear - 5;
        for (int i = 0; i < 11; i++) {
            yearList.add(thisYear);
            thisYear = thisYear + 1;
        }
        view.addObject("YearList", yearList);             // 设置年份选择列表数据
        view.setViewName("purchase.non_material_cost.edit.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表处理部分">
    /**
     * 根据标识取得非材料花费数据。
     *
     * @param criterion 非材料花费标识
     * @return 非材料花费数据（包括主表和子表）。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchByHeaderKey(criterion);
    }

    /**
     * 根据条件取得非材料花费信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 非材料花费信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/header/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchForPage(criterion);
    }

    /**
     * 保存非材料花费信息数据。
     * <p>
     * @param instance 要保存的非材料花费信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody final NonMaterialCostHeaderEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getHeaderId() > 0) { // 编辑
            rowsAffected = (long) _manager_header.update(instance);
        } else {                          // 新增
            rowsAffected = _manager_header.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除非材料花费信息数据。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="明细数据处理部分">
    /**
     * 新增、更新非材料花费明细信息数据。
     * <p>
     * @param criterion 非材料花费明细数据。
     * @return 影响行数和明细表记录.
     * @throws Exception
     */
    @RequestMapping(value = "/lines/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> savelines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.batchUpdate(criterion);
    }

    /**
     * 删除非材料花费明细。
     *
     * @param criterion 要删除的明细标识等
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.delete(criterion);
    }
    // </editor-fold>
}
